package com.grom.action;

import java.util.List;
import java.util.Map;

import com.dispacher.config.GlobalConfig;
import com.dispacher.context.ContextHolder;
import com.grom.po.GcmsUser;
import com.grom.po.Security;
import com.grom.service.GcmsUserService;
import com.grom.service.SecurityService;
import com.grom.util.Constants;

public class LoginAction {
	public void noLogin() {
		ContextHolder.setRequestAttribute("SYSID", "SALE");
		ContextHolder.forward("/WEB-INF/jsp/frame.jsp");
	}

	public void doLogin() {
		String userName = ContextHolder.findInstance().getRequest().getParameter("userName");
		String password = ContextHolder.findInstance().getRequest().getParameter("password");
		Map<String, List<Security>> allSecurity = SecurityService.instance.getAllSecurityMap();
		ContextHolder.setRequestAttribute("allSecurity", allSecurity);
		if (adminLogin(userName, password)) {
			ContextHolder.getCurSession().setAttribute("USER", "ADMIN");
			ContextHolder.setRequestAttribute("SYSID", "SYSMANAGE");
			List<Security> rootModules = SecurityService.instance.getAllRootSecurity();
			ContextHolder.setRequestAttribute("rootModules", rootModules);
			Security currentSecurity = SecurityService.instance.getSecurityByAction("SYSMANAGE");
			Map<Security, List<Security>> userSecuritys = SecurityService.instance
					.getAllSecurityByParentId(currentSecurity.getID());
			ContextHolder.setRequestAttribute("userSecuritys", userSecuritys);
			ContextHolder.forward("/WEB-INF/jsp/frame.jsp");
		} else if (userLogin(userName, password)) {
			ContextHolder.getCurSession().setAttribute("USER", userName);
			ContextHolder.setRequestAttribute("SYSID", "SALE");
			List<Security> rootModules = SecurityService.instance.getAllRootSecurityByUser(userName);
			ContextHolder.setRequestAttribute("rootModules", rootModules);
			Security currentSecurity = SecurityService.instance.getSecurityByAction("SALE");
			Map<Security, List<Security>> userSecuritys = SecurityService.instance
					.getAllSecurityByParentId(currentSecurity.getID());
			ContextHolder.setRequestAttribute("userSecuritys", userSecuritys);
			ContextHolder.forward("/WEB-INF/jsp/frame.jsp");
		} else {
			ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri());
		}
	}

	public void logOut() {
		ContextHolder.getCurSession().invalidate();
		ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri());
	}

	private boolean adminLogin(String userName, String password) {
		if (userName != null && password != null && userName.equals("admin")
				&& password.equals(GlobalConfig.getProperty("admin.password"))) {
			return true;
		} else {
			return false;
		}
	}

	private boolean userLogin(String userName, String password) {
		GcmsUser user = GcmsUserService.instance.getUserByUserName(userName);
		ContextHolder.getCurSession().setAttribute(Constants.KEY_USER_SESSION, user);
		return GcmsUserService.instance.isUserCorrect(userName, password);
	}
}
